//https://blog.csdn.net/zl1zl2zl3/article/details/81357146
import React, { PureComponent, Fragment } from 'react';
import { connect } from 'dva';
import moment from 'moment';
import router from 'umi/router';
import {
    Row,
    Col,
    Card,
    Form,
    Input,
    Icon,
    Button,
    message,
    Divider,
} from 'antd';
import StandardTable from '@/components/StandardTable';
import PageHeaderWrapper from '@/components/PageHeaderWrapper';

import ProSystemCreateForm from './ProSystemCreateForm';//新增表单
import ProSystemUpdateForm from './ProSystemUpdateForm';//修改表单
import ProSystemViewForm from './ProSystemViewForm';//查看表单

import styles from './ProSystemList.less';
import {getValue} from '@/utils/globalcommon';
const FormItem = Form.Item;


/* eslint react/no-multi-comp:0 */
@connect(({ prosystem, loading }) => ({
    prosystem,
    loading: loading.models.prosystem,
}))

@Form.create()
class ProSystemList extends PureComponent {
    state = {
        modalVisible: false,
        updateModalVisible: false,
        viewModalVisible: false,
        expandForm: false,
        selectedRows: [],
        formValues: {},
        updateFormValues: {},
        viewFormValues: {},
    };

    columns = [

       {
        title: '系统名称',
        dataIndex: 'name',
        key: 'name',
        },        {
        title: '英文名称',
        dataIndex: 'ename',
        key: 'ename',
        },      {
        title: '系统标志',
        dataIndex: 'logo',
        key: 'logo',
        },  {
        title: '版本',
        dataIndex: 'version',
        key: 'version',
        }, {
    title: '操作',
        render: (text, record) => (
<Fragment>
<a onClick={() => this.handleViewModalVisible(true, record)}>查看</a>
<Divider type="vertical" />
    <a onClick={() => this.handleUpdateModalVisible(true, record)}>编辑</a>
    <Divider type="vertical" />
    <a onClick={() => this.handleDeleteRecord(record)}>删除</a>
    </Fragment>
),
},
];


componentDidMount() {
    const { dispatch } = this.props;
    dispatch({
    	type: 'prosystem/query_page_for_prosystem',
    });
}



handleStandardTableChange = (pagination, filtersArg, sorter) => {
    const { dispatch } = this.props;
    const { formValues } = this.state;

    const filters = Object.keys(filtersArg).reduce((obj, key) => {
        const newObj = { ...obj };
	    newObj[key] = getValue(filtersArg[key]);
	    return newObj;
	}, {});

    const params = {
        currentPage: pagination.current,
        pageSize: pagination.pageSize,
        ...formValues,
        ...filters,
    };
    if (sorter.field) {
        params.sorter = `${sorter.field}_${sorter.order}`;
    }

    dispatch({
        type: 'prosystem/query_page_for_prosystem',
        payload: params,
    });
};

previewItem = id => {
    router.push(`/profile/basic/${id}`);
};

handleFormReset = () => {
    const { form, dispatch } = this.props;
    form.resetFields();
    this.setState({
        formValues: {},
    });
    dispatch({
        type: 'prosystem/query_page_for_prosystem',
        payload: {},
    });
};

toggleForm = () => {
    const { expandForm } = this.state;
    this.setState({
        expandForm: !expandForm,
    });
};


handleSelectRows = rows => {
    this.setState({
        selectedRows: rows,
    });
};

handleSearch = e => {
    e.preventDefault();

    const { dispatch, form } = this.props;

    form.validateFields((err, fieldsValue) => {
	    if (err) return;
	
	    const values = {
	        ...fieldsValue,
	        updatedAt: fieldsValue.updatedAt && fieldsValue.updatedAt.valueOf(),
	    };
	
	    this.setState({
	        formValues: values,
	    });
	
	    dispatch({
	        type: 'prosystem/query_page_for_prosystem',
	        payload: values,
	    });
	});
};

handleModalVisible = flag => {
    this.setState({
        modalVisible: !!flag,
    });
};

handleUpdateModalVisible = (flag, record) => {
    this.setState({
        updateModalVisible: !!flag,
        updateFormValues: record || {},
    });
};

handleViewModalVisible = (flag, record) => {
    this.setState({
        viewModalVisible: !!flag,
        viewFormValues: record || {},
    });
};

handleAdd = fields => {
    const { dispatch } = this.props;
    dispatch({
        type: 'prosystem/insert_for_prosystem',
        payload: {
            id: fields.id,
            name: fields.name,
            ename: fields.ename,
            contextpath: fields.contextpath,
            tableprefix: fields.tableprefix,
            logo: fields.logo,
            icon: fields.icon,
            version: fields.version,
            builddate: fields.builddate,
            status: fields.status,
            orderid: fields.orderid,
            memo: fields.memo,
        },
    });

    message.success('添加成功');
    this.handleModalVisible();

};


handleDeleteRecord = record => {
        const { dispatch } = this.props;

        dispatch({
	        type: 'prosystem/delete_by_primarykey_for_prosystem',
	        payload: {
	        	recordid: record.id,
	        },
	        callback: () => {
		        this.setState({
		        	selectedRows: [],
		        });
	        },
        });

        message.success('删除成功');


};

handleUpdate = fields => {

    const { dispatch } = this.props;
    const { formValues } = this.state;
    dispatch({
        type: 'prosystem/update_for_prosystem',
        payload: {
            id: fields.id,
            name: fields.name,
            ename: fields.ename,
            contextpath: fields.contextpath,
            tableprefix: fields.tableprefix,
            logo: fields.logo,
            icon: fields.icon,
            version: fields.version,
            builddate: fields.builddate,
            status: fields.status,
            orderid: fields.orderid,
            memo: fields.memo,
        },
    });

    message.success('修改成功');
    this.handleUpdateModalVisible();

};

renderSimpleForm() {
    const {
        form: { getFieldDecorator },
    } = this.props;
    return (
        <Form onSubmit={this.handleSearch} layout="inline">
        <Row gutter={{ md: 8, lg: 24, xl: 48 }}>
<Col md={8} sm={24}>
        <FormItem label="系统名称">
        {getFieldDecorator('name')(<Input placeholder="请输入" />)}
</FormItem>
    </Col>
   <Col md={8} sm={24}>
        <FormItem label="英文名称">
        {getFieldDecorator('ename')(<Input placeholder="请输入" />)}
</FormItem>
    </Col>
    <Col md={8} sm={24}>
        <span className={styles.submitButtons}>
        <Button type="primary" htmlType="submit">
        查询
        </Button>
        <Button style={{ marginLeft: 8 }} onClick={this.handleFormReset}>
        重置
        </Button>
        <a style={{ marginLeft: 8 }} onClick={this.toggleForm}>
        展开 <Icon type="down" />
        </a>
        </span>
        </Col>
        </Row>
        </Form>
);
}

renderAdvancedForm() {
    const {
        form: { getFieldDecorator },
    } = this.props;
    return (
        <Form onSubmit={this.handleSearch} layout="inline">
        <Row gutter={{ md: 8, lg: 24, xl: 48 }}>
<Col md={8} sm={24}>
        <FormItem label="系统名称">
        {getFieldDecorator('name')(<Input placeholder="请输入" />)}
</FormItem>
    </Col>
   <Col md={8} sm={24}>
        <FormItem label="英文名称">
        {getFieldDecorator('ename')(<Input placeholder="请输入" />)}
</FormItem>
    </Col>
    </Row>
    <div style={{ overflow: 'hidden' }}>
<div style={{ marginBottom: 24 }}>
<Button type="primary" htmlType="submit">
        查询
        </Button>
        <Button style={{ marginLeft: 8 }} onClick={this.handleFormReset}>
        重置
        </Button>
        <a style={{ marginLeft: 8 }} onClick={this.toggleForm}>
        收起 <Icon type="up" />
        </a>
        </div>
        </div>
        </Form>
);
}

    renderForm() {
        const { expandForm } = this.state;
        return expandForm ? this.renderAdvancedForm() : this.renderSimpleForm();
    }

    render() {
        const {
            prosystem: { data },
            loading,
        } = this.props;



        const { selectedRows, modalVisible, updateModalVisible, viewModalVisible,  updateFormValues,  viewFormValues  } = this.state;
        
        const parentMethods = {
            handleAdd: this.handleAdd,
            handleModalVisible: this.handleModalVisible,
        };
        const updateMethods = {
            handleUpdateModalVisible: this.handleUpdateModalVisible,
            handleUpdate: this.handleUpdate,
        };
        const viewMethods = {
            handleViewModalVisible: this.handleViewModalVisible,
        };
        return (
            <PageHeaderWrapper title="系统信息列表">
            <Card bordered={false}>
            <div className={styles.proSystemtableList}>
            <div className={styles.proSystemtableListForm}>{this.renderForm()}</div>
            <div className={styles.proSystemtableListOperator}>
            <Button icon="plus" type="primary" onClick={() => this.handleModalVisible(true)}>
        新增
        </Button>
    </div>
        <StandardTable
        rowKey={"id"}
        selectedRows={selectedRows}
        loading={loading}
        data={data}
        columns={this.columns}
        onSelectRow={this.handleSelectRows}
        onChange={this.handleStandardTableChange}
        />
        </div>
        </Card>

        <ProSystemCreateForm {...parentMethods} modalVisible={modalVisible} />

        {updateFormValues && Object.keys(updateFormValues).length ? (
            <ProSystemUpdateForm
            {...updateMethods}
            updateModalVisible={updateModalVisible}
            values={updateFormValues}
            />
        ) : null}

        {viewFormValues && Object.keys(viewFormValues).length ? (
            <ProSystemViewForm
            {...viewMethods}
            viewModalVisible={viewModalVisible}
            values={viewFormValues}
            />
        ) : null}

    </PageHeaderWrapper>
    );
    }
}

export default ProSystemList;